package com.yjd.comm.base.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

import com.yjd.comm.util.ServletUtil;
import com.yjd.comm.util.ServletUtil.UrlTypeEnum;

/**
 * <b>description</b>：获取路径的url <br>
 * <b>time</b>：2014-11-7下午1:39:51 <br>
 * <b>author</b>： ready likun_557@163.com
 */
public class UrlTag extends BodyTagSupport {
	private static final long serialVersionUID = 1L;
	/**
	 * url
	 */
	private String url;

	/**
	 * url类型
	 */
	private Integer type;

	/**
	 * 是否加后缀
	 */
	private boolean hashStuff = true;

	@Override
	public int doStartTag() throws JspException {
		try {
			if (type != null) {
				UrlTypeEnum ut = UrlTypeEnum.get(type);
				String tempurl = ServletUtil.getUrl1(
						this.url,
						ut,
						null,
						hashStuff && ut.getAppendStuff() ? ServletUtil
								.getUrlStuff() : null);
				String rurl = ServletUtil.getRequestUrl(
						(HttpServletRequest) this.pageContext.getRequest())
						.toString();
				if (UrlTypeEnum.PICS.getValue().equals(type)
						|| UrlTypeEnum.STATIC.getValue().equals(type)) {
					if ((rurl.startsWith("HTTPS") || rurl.startsWith("https"))
							&& tempurl.startsWith("http")) {
						tempurl = "https" + tempurl.substring(4);
					}
				}
				this.pageContext.getOut().write(tempurl);
			} else {
				this.pageContext.getOut().write(ServletUtil.getUrl(url));
			}
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
		return SKIP_BODY;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	public boolean isHashStuff() {
		return hashStuff;
	}

	public void setHashStuff(boolean hashStuff) {
		this.hashStuff = hashStuff;
	}

}
